package com.gl.doc.operation.comment.impl;

import com.gl.doc.model.WordComment;
import com.gl.doc.operation.comment.BindComment;
import com.gl.doc.operation.comment.DoBindComment;
import com.gl.doc.operation.comment.entity.CommentLocation;
import com.gl.doc.operation.comment.entity.TargetSentenceIndex;
import com.gl.doc.operation.comment.entity.TargetSubParagraphContentIndex;
import com.gl.doc.operation.comment.entity.WordCommentRelation;
import com.gl.doc.support.XmlSupport;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.List;

/**
 * 默认实现绑定批注调度类
 */
@Data
@Slf4j
public class DefaultBindComment implements BindComment, DoBindComment {
    /**
     * 按条件绑定批注接口实现类对象
     * CaseOneBindComment 第一种段落里面只有一个子段落
     * CaseSecondBindComment 第二种段落里面有两个以上子段落
     */
    private List<DoBindComment> doBindComments;

    @Override
    public void addParagraphComment(WordCommentRelation wordCommentRelation) {
        //先解析这个需要加批注的 文字 在段落里面的位置
        WordComment wordComment = wordCommentRelation.getWordComment();
        CommentLocation commentLocation = new CommentLocation();
        commentLocation.setWordComment(wordComment);
        List<TargetSentenceIndex> targetSentenceIndexList = commentLocation.getTargetSentenceIndexList();
        for (TargetSentenceIndex targetSentenceIndex : targetSentenceIndexList) {
            List<TargetSubParagraphContentIndex> targetSubParagraphContentIndexList = commentLocation.getTargetSubParagraphContentIndexList();
            for (DoBindComment doBindComment : doBindComments) {
                wordCommentRelation.setTargetSentenceIndex(targetSentenceIndex);
                wordCommentRelation.setTargetSubParagraphContentIndexList(targetSubParagraphContentIndexList);
                doBindComment.doAddParagraphComment(wordCommentRelation);
            }
        }
    }

    /**
     * 绑定批注内容
     *
     * @param comStartIndex 批注开始位置
     * @param comEndIndex   批注结束位置
     * @param comRefIndex   批注引用位置
     * @param ps            word文档段落的子段落xml集合
     * @param wordComment   批注对象
     */
    protected void doBindComment(Integer comStartIndex, Integer comEndIndex, Integer comRefIndex, List<Element> ps, WordComment wordComment) {
        ps.add(comStartIndex, DocumentHelper.createElement("w:commentRangeStart").addAttribute("w:id", wordComment.getId()));
        ps.add(comEndIndex, DocumentHelper.createElement("w:commentRangeEnd").addAttribute("w:id", wordComment.getId()));
        Element wr = DocumentHelper.createElement("w:r");
        Element wRPr = DocumentHelper.createElement("w:rPr");
        Element wrStyle = DocumentHelper.createElement("w:rStyle").addAttribute("w:val", "a3");
        Element wCr = DocumentHelper.createElement("w:commentReference").addAttribute("w:id", wordComment.getId());
        wRPr.add(wrStyle);
        wr.add(wRPr);
        wr.add(wCr);
        ps.add(comRefIndex, wr);
    }

    /**
     * 添加批注内容到批注xml文件
     *
     * @param wordComment 批注对象
     * @param commentFile 批注xml文件
     */
    protected void addComment(WordComment wordComment, File commentFile) {
        try {
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(commentFile);
            Element rootElement = document.getRootElement();
            List<Element> elements = rootElement.elements();
            wordComment.setId(String.valueOf(elements.size()));
            Element wComment = rootElement.addElement("w:comment");
            wComment.addAttribute("w:id", wordComment.getId());
            wComment.addAttribute("w:author", wordComment.getAuthor());
            wComment.addAttribute("w:date", wordComment.getDate());
            wComment.addAttribute("w:initials", "AI");
            Element wP = wComment.addElement("w:p");
            Element wPPr = wP.addElement("w:pPr");
            wPPr.addElement("w:pStyle").addAttribute(" w:val", "a4");
            Element wR1 = wP.addElement("w:r");
            wR1.addElement("w:rPr").addElement("w:rStyle").addAttribute(" w:val", "a3");
            wR1.addElement("w:annotationRef");
            Element wR = wP.addElement("w:r");
            Element wT = wR.addElement("w:t");
            wT.setText(wordComment.getContent());
            XmlSupport.writer(document, commentFile);
        } catch (Exception e) {
            log.error("word生成批注失败!", e);
        }
    }

    /**
     * 按条件绑定批注默认空实现由子类实现
     *
     * @param wordCommentRelation word批注关系对象
     */
    @Override
    public void doAddParagraphComment(WordCommentRelation wordCommentRelation) {

    }
}
